home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / games / wordy313.zip / SUFFIX.C < prev    next >
C/C++ Source or Header  |  1995-04-30  |  6KB  |  213 lines

  1. /**************************************************************************/
  2. /*                             Suffix  Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".suf"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34.  
  35. #define BUFFERSIZE 8192
  36.  
  37. char ad[] =
  38. "SUFFIX utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  39.  
  40. typedef enum { FALSE, TRUE } Boolean;
  41.  
  42. void getword( char *lset );
  43. void center( char *strng );
  44. Boolean wordtest( char *lset, char *wd, size_t len );
  45.  
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char letterset[ MAXLEN ];
  51.  
  52.      if( argc == NOARGS )
  53.         {
  54.         clrscr();
  55.         puts("Enter a LETTERSET to test ... ");
  56.         gets( letterset );
  57.         }
  58.      else
  59.         strcpy( letterset, *( argv + 1 ) );
  60.  
  61.      getword( letterset );
  62. }
  63.  
  64.  
  65.  
  66. Boolean wordtest( char *letterset, char *word, size_t l_len )
  67. {
  68.     Boolean error_flag;
  69.  
  70.       if( !strncmp( letterset, word + strlen( word ) - 1 - l_len, l_len ) )
  71.          error_flag = TRUE;
  72.       else
  73.          error_flag = FALSE;
  74.         return( error_flag );
  75. }
  76.  
  77. /*************************************************************/
  78. void getword( char *letter_set )
  79. {
  80.  
  81.     char    l_set [ MAXLEN ],
  82.         word [ MAXLEN ],
  83.         tempstr [ MAXLEN + 1 ],
  84.         targetfile [ MAXLEN ],
  85.         bar [ LINE_LEN + 1 ],
  86.         double_bar [ LINE_LEN + 1 ];
  87.    size_t l_len;
  88.  
  89.     FILE *fptr,
  90.         *tfile;
  91.     int fnamelen;
  92.     long wcount = 0L;
  93.  
  94.  
  95.       l_len = strlen( letter_set );
  96.        memset( bar, '-', LINE_LEN );
  97.        *( bar + LINE_LEN ) = NULL;
  98.        memset( double_bar, '=', LINE_LEN );
  99.        *( double_bar + LINE_LEN ) = NULL;
  100.  
  101.        /*************opening credits*************/
  102.        clrscr();
  103.        printf( double_bar );
  104.        strcpy( tempstr, ad );
  105.        center ( tempstr );
  106.        printf( tempstr );
  107.        printf( CR );
  108.        printf( double_bar );
  109.        printf( CR );
  110.        /****************************************/
  111.  
  112.  
  113.        strcpy ( l_set, letter_set );
  114. //       strcat ( letter_set, CR );
  115.  
  116.        /*   Create name of file to store derived words in   */
  117.        /*********************************************************/
  118.        fnamelen = strlen( l_set );
  119.        if( fnamelen  > FILENAME_MAXLEN )
  120.           fnamelen = FILENAME_MAXLEN;
  121.        strncpy( targetfile, l_set, fnamelen );
  122.        *( targetfile + fnamelen ) = NULL;
  123.        //NULL-terminate string, so strcat works, ha, ha.
  124.        strcat( targetfile, FILE_SUFFIX );
  125.        /*********************************************************/
  126.  
  127.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  128.          {
  129.          printf( "\7\7\7Cannot open Wordfile!" );
  130.          exit( FILE_OPENING_ERROR );
  131.          }
  132.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  133.          exit( FILE_OPENING_ERROR + 1 );
  134.  
  135.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  136.          {
  137.          printf( "\7\7\7Cannot open file to save words in!" );
  138.          exit ( FILE_OPENING_ERROR + 2 );
  139.          }
  140.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  141.          exit( FILE_OPENING_ERROR + 3 );
  142.  
  143.        /**************'Wait' Message************/
  144.        printf( CR CR );
  145.        printf( "WORKING...\n\n" );
  146.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  147.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  148.        printf(
  149. "Now searching 99,000+ word file \nand writing file of valid words suffixed -%s-.\n\n",
  150.             letter_set );
  151.        /*****************************************/
  152.  
  153.  
  154.  
  155.  
  156.  
  157.        sprintf( tempstr, "Words created from: %s\n", strupr( l_set ) );
  158.        center( tempstr );
  159.        fprintf( tfile, double_bar );
  160.       fprintf( tfile, CR );
  161.        fprintf( tfile, tempstr );
  162.        fprintf( tfile, double_bar );
  163.        fprintf( tfile, CR );
  164.  
  165.  
  166.          /*********************Main Loop*************/     
  167.           while( fgets( word, MAXLEN, fptr ) != NULL )
  168.  
  169.             if( wordtest( letter_set, word, l_len ) )
  170.                {
  171.                fprintf( tfile, "%s", word );
  172.                wcount++;
  173.                }
  174.           /*******************************************/
  175.  
  176.           fprintf( tfile, bar );
  177.       fprintf( tfile, CR );
  178.           sprintf( tempstr, "%ld words prefixed %s have been found.",
  179.                  wcount, l_set );
  180.           center( tempstr );              
  181.           fprintf( tfile, tempstr );
  182.           fprintf( tfile, "\n\n" );
  183.  
  184.           center( ad );
  185.           fprintf( tfile, ad );
  186.  
  187.           fcloseall();
  188.  
  189.           sprintf( tempstr,
  190.                  "The file %s has %ld words suffixed by %s\7.",
  191.                  targetfile, wcount, l_set );
  192.           center( tempstr );
  193.           printf( CR CR );
  194.           printf( tempstr );
  195.  
  196. }
  197.  
  198.  
  199.  
  200. void center( char *str )
  201. {
  202.    int padding;
  203.    char st [ LINE_LEN + INCREMENT ];
  204.  
  205.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  206.      memset( st, SPACE, padding );
  207.      *( st + padding ) = NULL;  //Terminate string
  208.      strcat( st, str );
  209.      strcpy( str, st );
  210.  
  211.      return;
  212. }
  213.